<?php

namespace Modules\Admin\Http\Controllers;

use App\Services\AdminService;
use App\Services\TaskLogService;
use App\Services\TaskService;
use Validator;

class TaskController extends Controller
{
    protected $taskService;
    protected $taskLogService;
    protected $adminService;

    public function __construct(
        TaskService $taskService,
        TaskLogService $taskLogService,
        AdminService $adminService
    )
    {
        $this->taskService = $taskService;
        $this->taskLogService = $taskLogService;
        $this->adminService = $adminService;
    }

    /**
     * 任务列表
     */
    public function getList()
    {
        try {
            if ($res = $this->taskService->getList($this->input('search'), $this->input('limit', 20))) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 任务详情
     */
    public function detail($id)
    {
        try {
            if ($res = $this->taskService->detail($id)) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 添加任务
     */
    public function add()
    {
        try {
            $requestData = request()->all();
            $requestData['process'] = $requestData['user_id']?'待开始':'未指派';
            if ($requestData['user_id']) {
                $requestData['assigned_at'] = date('Y-m-d H:i:s');
            }

            $validator = Validator::make($requestData, [
                'filename' => 'required',
                'attach_id' => 'required',
                'importance' => 'required',
            ]);

            if ($validator->fails()) {
                return $this->ajaxReturn(1002, $validator->errors()->first());
            }

            if ($obj = $this->taskService->add($requestData)) {
                $user = $this->adminService->member();
                $this->taskLogService->add([
                    'task_id' => $obj->id,
                    'user_id' => $user->admin_id,
                    'username' =>$user->username,
                    'process' => $obj->process,
                    'remark' => "{$user->username} 新建了任务 {$obj->id}",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 修改任务
     */
    public function edit($id)
    {
        try {
            $requestData = request()->all();
            $validator = Validator::make($requestData, [
                'filename' => 'required',
                'attach_id' => 'required',
                'importance' => 'required',
            ]);

            if ($validator->fails()) {
                return $this->ajaxReturn(1002, $validator->errors()->first());
            }

            if (!$requestData['user_id']) {
                $requestData['assigned_at'] = null;
                $requestData['process'] = '未指派';
            }

            if ($res = $this->taskService->edit($id, $requestData)) {
                $user = $this->adminService->member();
                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $user->admin_id,
                    'username' =>$user->username,
                    'process' => $res->process,
                    'remark' => "{$user->username} 编辑了任务 {$id}",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 删除任务
     */
    public function remove($id)
    {
        try {
            if ($this->taskService->remove($id)) {

                $user = $this->adminService->member();
                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $user->admin_id,
                    'username' =>$user->username,
                    'process' => '已取消',
                    'remark' => "{$user->username} 删除了任务 {$id}",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 我的任务
     */
    public function my()
    {
        try {
            $search = $this->input('search');
            $search['user_id'] = $this->adminService->member()->admin_id;
            if ($res = $this->taskService->getList($search, $this->input('limit', 20))) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 指派任务
     */
    public function assign($id)
    {
        try {
            if ($res = $this->taskService->assign($id, request()->all())) {

                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $res->user_id,
                    'username' =>$res->username,
                    'process' => '待开始',
                    'remark' => "任务 {$id} 指派给了 {$res->username}",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 取消任务
     */
    public function cancel($id)
    {
        try {
            if ($res = $this->taskService->cancel($id)) {

                $user = $this->adminService->member();
                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $user->admin_id,
                    'username' =>$user->username,
                    'process' => '已取消',
                    'remark' => "{$user->username} 取消了任务 {$id}",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 开始任务
     */
    public function start($id)
    {
        try {
            if ($res = $this->taskService->start($id, $this->adminService->member()->admin_id)) {

                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $res->user_id,
                    'username' =>$res->username,
                    'process' => '进行中',
                    'remark' => "{$res->username} 开始了 {$id} 任务",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 完成任务
     */
    public function finish($id)
    {
        try {
            if ($res = $this->taskService->finish($id, $this->adminService->member()->admin_id, request()->all())) {

                $this->taskLogService->add([
                    'task_id' => $id,
                    'user_id' => $res->user_id,
                    'username' =>$res->username,
                    'process' => '已完成',
                    'remark' => "{$res->username} 完成了 {$id} 任务",
                    'created_at' => date('Y-m-d H:i:s'),
                ]);

                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * 任务日志
     */
    public function log($id)
    {
        try {
            $res = $this->taskLogService->getList(['task_id' => $id]);

            return $this->ajaxReturn(0, '', $res);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     *
     */
    public function info()
    {
        try {
            if ($res = $this->taskService->info()) {

                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }
}
